package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.FriendShipLinkDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.FriendShipLink;
import com.jeff.tianti.cms.service.FriendShipLinkService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.common.util.Md5Util;
import com.jeff.tianti.org.entity.Role;
import com.jeff.tianti.org.entity.User;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/link")
public class FriendShipLinkController {
    @Autowired
    FriendShipLinkService friendShipLinkService;
    /**
     * 获取 友联列表
     * @return
     */
    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model){
        String siteName = request.getParameter("siteName");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage = 1;
        int pageSize = 10;
        if(StringUtils.isNotBlank(currentPageStr)){
            currentPage = Integer.parseInt(currentPageStr);
        }
        if(StringUtils.isNotBlank(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        FriendShipLinkDTO friendShipLinkDTO = new FriendShipLinkDTO();
        friendShipLinkDTO.setSiteName(siteName);
        friendShipLinkDTO.setCurrentPage(currentPage);
        friendShipLinkDTO.setPageSize(pageSize);

        PageModel<FriendShipLink> page = friendShipLinkService.queryFriendShipLinkPage(friendShipLinkDTO);
        model.addAttribute("page", page);
        model.addAttribute("friendShipLinkQueryDTO", friendShipLinkDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_FRIENDLY_LINK);
        return "/cms/link/friend_link_list";
    }

    /**
     * 跳转到友情编辑页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/dialog/edit")
    public String dialogEdit(HttpServletRequest request, Model model){

        String id = request.getParameter("id");
        if(StringUtils.isNotBlank(id)){
            FriendShipLink friendShipLink = friendShipLinkService.find(id);
            model.addAttribute("friendShipLink", friendShipLink);
        }

        return "/cms/link/dialog/friend_link_edit";
    }

    /**
     * 栏目保存操作
     * @param request
     * @return
     */
    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {

            String id = request.getParameter("id");
            String siteName = request.getParameter("siteName");
            String link = request.getParameter("link");
            FriendShipLink friendShipLink = new FriendShipLink();
            friendShipLink.setSiteName(siteName);
            friendShipLink.setLink(link);
            friendShipLink.setId(id);
            if(StringUtils.isNotBlank(id)){
                friendShipLinkService.update(friendShipLink);
            }else{
                friendShipLinkService.save(friendShipLink);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

 /**
     * 栏目保存操作
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            friendShipLinkService.delete(id);
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
